<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <style>
        .container{
            margin: 0 auto;
            margin-top: 70px;
            text-align: center;
            width:300px;
        }
        .subTitle{
            color: gray;
            font-size: 14px;
        }
        .title{
            font-size: 45px;
        }
        .input {
            width: 90%;
        }
        .inputContainer {
            text-align: left;
            margin-bottom: 20px;
        }
        .subContainer {
            text-align: left;
        }
        .field {
            font-size:14px;
        }
        .input {
            border-radius: 6px;
            height:25px;
            margin-top:10px;
            border-color:silver;
            border-style:solid;
            background-color: cornsilk;
        }
        .tip {
            margin-top:5px;
            font-size:12px;
            color:gray;
        }
        .setting {
            font-size:9px;
            color:black;
        }
        .label {
            font-size:12px;
            margin-left: 5px;
            height: 20px;
            vertical-align: middle;
        }
        .checkbox {
            height: 20px;
            vertical-align: middle;
        }
        .btn {
            border-radius: 10px;
            height: 40px;
            width:300px;
            margin-top:30px;
            background-color: deepskyblue;
            border-color: blue;
            color:white;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="container">
            <div class="subTitle">加入我们，一起玩转Vue3</div>
            <h1 class="title">创建你的账号</h1>
            <div v-for="(item,index) in fields" class="inputContainer">
                <div class="field">{{item.title}}<span v-if="item.required" style="color:red">*</span></div>
                <!-- 元素是动态渲染出来的这里绑定数据也需要动态绑定 -->
                <input v-model="item.model" class="input" :type="item.type" />
                <div class="tip" v-if="index===2">请确认密码需要大于等于6位</div>
            </div>
            <div class="subContainer">
                <div class="setting">偏好设置</div>
                <input v-model="receiveMsg" class="checkbox" type="checkbox" /><label class="label">接收更新邮件</label>
            </div>
            <button @click="createAccount" class="btn">创建账号</button>
        </div>
    </div>
<script>
    const {createApp,ref,computed} = Vue
    const config = {
        setup() {
            const fields = ref([
                {title: '用户名', required:true,type:'text',model:""},
                {title: '邮箱地址', required:false,type:'text',model:""},
                {title: '密码', required:true,type:'password',model:""},

            ])
            const receiveMsg = ref(false)

            // 计算属性
            const name = computed({
                get(){
                    return fields.value[0].model
                },
                set(newValue){
                    fields.value[0].model = newValue
                }
            })
            const email = computed({
                get(){
                    return fields.value[1].model
                },
                set(newValue){
                    fields.value[1].model = newValue
                }
            })
            const password = computed({
                get(){
                    return fields.value[2].model
                },
                set(newValue){
                    fields.value[2].model = newValue
                }
            })

            // 对邮箱进行验证
            const emailCheck = () => {
                const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
                return reg.test(email.value);
            }
            // 注册方法
            const createAccount = () => {
                if (name.value.length === 0) {
                    alert('请输入用户名')
                    return
                } else if (password.value.length < 6) {
                    alert('密码设置需大于等于6位字符')
                    return
                } else if (email.value.length > 0 && !emailCheck()) {
                    alert('请输入正确的邮箱')
                    return
                } else {
                    alert('注册成功')
                }
            }

            return {fields,receiveMsg,name,email,password,createAccount}
        }
    }

    const app = createApp(config)
    app.mount('#app')
</script>
</body>
</html>